package org.amphiprion.whohavethekids;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;

import org.amphiprion.kidcalendar.dao.xml.ConfigurationDao;
import org.amphiprion.kidcalendar.dao.xml.FunctionDao;
import org.amphiprion.kidcalendar.entity.Configuration;
import org.amphiprion.kidcalendar.entity.function.Function;
import org.amphiprion.kidcalendar.entity.function.Input;
import org.amphiprion.whohavethekids.query.view.builder.QueryInputBuilder;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class QueryActivity extends Activity {
	private HashMap<String, Object> instanceOfClass = new HashMap<String, Object>();
	private Function currentFunction;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_query);
		final LinearLayout pnQuery = (LinearLayout) findViewById(R.id.pnQueryInput);
		final Configuration configuration = ConfigurationDao.getInstance().getConfiguration();
		final List<Function> functions = FunctionDao.getInstance(this).getFunctions();
		final Spinner cbFunction = (Spinner) findViewById(R.id.cbQueryFunctions);
		currentFunction = null;
		cbFunction.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				currentFunction = (Function) cbFunction.getSelectedItem();
				rebuiltQueryScreen(pnQuery, configuration, currentFunction);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				currentFunction = null;
			}
		});
		cbFunction.setAdapter(new ArrayAdapter<Function>(this, android.R.layout.simple_list_item_1, functions));
		//
		Button btExecute = (Button) findViewById(R.id.btLaunchQuery);
		btExecute.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				System.out.println("Click Execute");
				if (currentFunction != null) {
					Class[] paramsType = new Class[currentFunction.getInputs().size()];
					Object[] params = new Object[currentFunction.getInputs().size()];
					int i = 0;
					for (Input input : currentFunction.getInputs()) {
						QueryInputBuilder builder = (QueryInputBuilder) instanceOfClass.get(input.getClassName());
						if (!builder.isInputValid()) {
							return;
						}
						params[i] = builder.getInput();
						paramsType[i] = params[i].getClass();
						i++;
					}

					try {
						System.out.println(currentFunction.getClassName() + "." + currentFunction.getMethodName());
						Class cls = Class.forName(currentFunction.getClassName());
						Method m = cls.getMethod(currentFunction.getMethodName(), paramsType);
						Object result = m.invoke(null, params);
						Toast.makeText(QueryActivity.this, ">> " + result, Toast.LENGTH_LONG).show();
					} catch (Throwable e) {
						e.printStackTrace();
					}
				}
			}
		});
	}

	protected void rebuiltQueryScreen(LinearLayout pnQuery, Configuration configuration, Function function) {
		LayoutInflater inflater = LayoutInflater.from(this);
		pnQuery.removeAllViews();
		try {
			for (Input input : function.getInputs()) {
				QueryInputBuilder builder = null;
				if (instanceOfClass.containsKey(input.getClassName())) {
					builder = (QueryInputBuilder) instanceOfClass.get(input.getClassName());
				} else {
					Class cls = Class.forName(input.getClassName());
					builder = (QueryInputBuilder) cls.newInstance();
					instanceOfClass.put(input.getClassName(), builder);
				}
				if (!input.isHideTitle()) {
					View v = inflater.inflate(R.layout.query_input_header, null);
					TextView tvTitle = (TextView) v.findViewById(R.id.header_text);
					tvTitle.setText(input.getName());
					pnQuery.addView(v);
				}
				builder.build(this, pnQuery, configuration);
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.query, menu);
		return true;
	}

}
